package com.punch.course.controller;


import com.punch.common.dto.course.AdminEditClassDTO;
import com.punch.common.dto.course.ApplyCourseDTO;
import com.punch.common.dto.course.ClassDTO;
import com.punch.common.dto.course.ClassSaveOrUpdateDTO;
import com.punch.common.model.Result;
import com.punch.common.page.PageInfo;
import com.punch.common.vo.course.classes.ClassVO;
import com.punch.common.vo.course.classes.ClassPageVO;
import com.punch.course.exception.CourseExceptionEnum;
import com.punch.course.query.ClassQueryInfo;
import com.punch.course.service.CourseClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 课程班级表 前端控制器
 * </p>
 *
 * @author wjj
 * @since 2020-12-01
 */
@RestController
@Api(value = "CourseClassController", tags = "班级管理Controller")
public class CourseClassController {

    @Resource
    private CourseClassService courseClassService;

    /**
     * 条件分页查询班级列表
     *
     * @param classQueryInfo 查询条件
     * @return PageInfo<ClassPageVO>
     */
    @GetMapping(value = "/classes", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "条件分页查询班级列表", notes = "条件分页查询班级列表", produces = "application/json")
    public Result<PageInfo<ClassPageVO>> listClass(@ModelAttribute ClassQueryInfo classQueryInfo) {
        return Result.succeed(courseClassService.listClass(classQueryInfo));
    }


    /**
     * 查询班级详情
     *
     * @param classId 课程班级id
     * @return ClassVO
     */
    @GetMapping(value = "/class/{classId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询班级详情", notes = "查询班级详情", produces = "application/json")
    public Result<ClassVO> getClasses(@PathVariable Long classId) {
        return Result.succeed(courseClassService.getClasses(classId));
    }

    /**
     * 新增课程班级
     *
     * @param classSaveOrUpdateDTO 班级新增/修改表单
     * @return 主键id
     */
    @PostMapping(value = "/class", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增课程班级", notes = "新增课程班级", produces = "application/json")
    public Result<Long> saveClass(@Valid @RequestBody ClassSaveOrUpdateDTO classSaveOrUpdateDTO) {
        Long classId = courseClassService.saveClass(classSaveOrUpdateDTO);
        return Result.succeed(classId, CourseExceptionEnum.CLASS_SAVE_SUCCESS.getMsg());
    }


    /**
     * 修改未班级信息
     *
     * @param classId              班级id
     * @param classSaveOrUpdateDTO 班级新增/修改表单
     * @return 主键id
     */
    @PutMapping(value = "/class/un-release/{classId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改班级信息", notes = "修改班级信息", produces = "application/json")
    public Result<Long> updateUnReleaseClass(@PathVariable Long classId, @Valid @RequestBody ClassSaveOrUpdateDTO classSaveOrUpdateDTO) {
        courseClassService.updateUnReleaseClass(classId, classSaveOrUpdateDTO);
        return Result.succeed(classId, CourseExceptionEnum.CLASS_EDIT_SUCCESS.getMsg());
    }

    /**
     * 删除班级信息
     *
     * @param classId 班级id
     * @return 删除提示消息
     */
    @DeleteMapping(value = "/class/{classId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除班级信息", notes = "删除班级信息", produces = "application/json")
    public Result<String> deleteClass(@PathVariable Long classId) {
        courseClassService.deleteClass(classId);
        return Result.succeed(CourseExceptionEnum.CLASS_DEL_SUCCESS.getMsg());
    }


    /**
     * 机构申请开班
     *
     * @param applyCourseDTO 开班表单
     * @return void
     */
    @PutMapping(value = "/class/apply/school", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改班级信息", notes = "修改班级信息", produces = "application/json")
    public Result applySchoolCourse(@RequestBody ApplyCourseDTO applyCourseDTO) {
        courseClassService.applySchoolCourse(applyCourseDTO);
        return Result.succeed(CourseExceptionEnum.CLASS_EDIT_SUCCESS.getMsg());
    }

    /**
     * 机构取消申请开班
     *
     * @param classId 班级id
     * @return void
     */
    @DeleteMapping(value = "/class/apply/school/cancel/{classId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "机构取消申请开班", notes = "机构取消申请开班", produces = "application/json")
    public Result applySchoolCourseCancel(@PathVariable Long classId) {
        courseClassService.applySchoolCourseCancel(classId);
        return Result.succeed(CourseExceptionEnum.CLASS_EDIT_SUCCESS.getMsg());
    }

    /**
     * 发布班级
     *
     * @param classId  班级id
     * @param classDTO 班级发布
     * @return 主键id
     */
    @PutMapping(value = "/class/release/{classId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改班级信息", notes = "修改班级信息", produces = "application/json")
    public Result<Long> releaseClass(@PathVariable Long classId, @Valid @RequestBody ClassDTO classDTO) {
        courseClassService.releaseClass(classId, classDTO);
        return Result.succeed(classId, CourseExceptionEnum.CLASS_EDIT_SUCCESS.getMsg());
    }

    /**
     * 取消报名
     *
     * @param classId 班级id
     * @return 主键id
     */
    @PutMapping(value = "/class/sign-up-cancel/{classId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改班级信息", notes = "修改班级信息", produces = "application/json")
    public Result<Long> signUpCancel(@PathVariable Long classId) {
        courseClassService.signUpCancel(classId);
        return Result.succeed(classId, CourseExceptionEnum.CLASS_EDIT_SUCCESS.getMsg());
    }


    /**
     * 拒绝申请
     *
     * @param classId 班级id
     * @return 主键id
     */
    @PutMapping(value = "/class/refuse-apply/{classId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "拒绝申请", notes = "拒绝申请", produces = "application/json")
    public Result<Long> refuseApply(@PathVariable Long classId) {
        courseClassService.refuseApply(classId);
        return Result.succeed(classId, CourseExceptionEnum.CLASS_EDIT_SUCCESS.getMsg());
    }


    /**
     * 运营修改班级
     *
     * @param classId           班级id
     * @param adminEditClassDTO 运营修改班级dto
     * @return 主键id
     */
    @PutMapping(value = "/class/admin/{classId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "运营修改班级", notes = "运营修改班级", produces = "application/json")
    public Result<Long> adminEditClass(@PathVariable Long classId, @Valid @RequestBody AdminEditClassDTO adminEditClassDTO) {
        courseClassService.adminEditClass(classId, adminEditClassDTO);
        return Result.succeed(classId, CourseExceptionEnum.CLASS_EDIT_SUCCESS.getMsg());
    }


    /**
     * 报名修改班级课程的报名人数
     *
     * @param courseClassId 班级课程id
     */
    @PutMapping(value = "/class/{courseClassId}/sign-up-number", produces = "application/json; charset=utf-8")
    public Boolean updateSignUpNumber(@PathVariable("courseClassId") Long courseClassId) {
        return courseClassService.updateSignUpNumber(courseClassId);
    }
}
